/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package drawapplication;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author NguyenViet
 */
public class DrawApplication {

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) throws Exception {
        JFrame f = new JFrame("Draw a Red Line");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(300, 300);
        f.setLocation(300, 300);
        f.setResizable(false);
        JPanel line;
        line = new JPanel() {
            Point pointStart = null;
            Point pointEnd = null;

            {
                addMouseListener(new MouseAdapter() {
                    @Override
                    public void mousePressed(MouseEvent e) {
                        pointStart = e.getPoint();
                    }

                    @Override
                    public void mouseReleased(MouseEvent e) {
                        pointStart = null;
                    }
                });
                addMouseMotionListener(new MouseMotionAdapter() {
                    @Override
                    public void mouseMoved(MouseEvent e) {
                        pointEnd = e.getPoint();
//                        System.out.println(e.getPoint().toString());
                    }

                    @Override
                    public void mouseDragged(MouseEvent e) {
                        pointEnd = e.getPoint();
                        repaint();
                    }
                });
            }

            @Override
            public void paint(Graphics g) {
                super.paint(g);
                if (pointStart != null) {
                    g.setColor(Color.RED);
                    g.drawLine(pointStart.x, pointStart.y, pointEnd.x, pointEnd.y);
                }
            }
        };
        JPanel rect = new JPanel() {
            Point A = null;
            Point B = null;
            Point C = null;
            Point D = null;
            double width;
            double height;

            {
                addMouseListener(new MouseAdapter() {
                    public void mousePressed(MouseEvent e) {
                        A = e.getPoint();
                    }

                    public void mouseReleased(MouseEvent e) {
                        A = null;
                    }
                });
                addMouseMotionListener(new MouseMotionAdapter() {
                    public void mouseMoved(MouseEvent e) {
                        B = e.getPoint();
                    }

                    public void mouseDragged(MouseEvent e) {
                        B = e.getPoint();
                        C = new Point(A.x, B.y);
                        D = new Point(B.x, A.y);

                        width = Math.sqrt(Math.pow(D.x - A.x, 2) + Math.pow(D.y - A.y, 2));
                        height = Math.sqrt(Math.pow(C.x - A.x, 2) + Math.pow(C.y - A.y, 2));

                        repaint();
                    }
                });
            }

            public void paint(Graphics g) {
                super.paint(g);
                if (A != null) {
                    g.setColor(Color.RED);
                    g.drawRect(A.x, A.y, (int) width, (int) height);
                }
            }
        };

        f.add(rect);
        f.setVisible(true);
    }
}
